package com.memoryangelapplication.sound;

import com.memoryangelapplication.AlertMessageActivity;
import com.memoryangelapplication.R;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.CountDownTimer;
import android.os.Handler;
import android.speech.tts.TextToSpeech;

public class PlaySoundClip implements TextToSpeech.OnInitListener {

	int eventSoundGap = 1;
	TextToSpeech talker;
	Activity activity;
	Context context;
	MediaPlayer player;
	String evtTime;

	SoundPool sp;
	int iTmp, iTmp1, iTmp2;

	public PlaySoundClip(Context context, Activity activity, String passTime) {
		this.activity = activity;
		this.context = context;
		this.evtTime = passTime;
	}

	public void playClip() {

		activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);

		player = MediaPlayer.create(context, R.raw.alarm);

		talker = new TextToSpeech(context, this);

		player.start();

		player.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				mp.stop();
				mp.release();
				Handler handler = new Handler();
				final Runnable r = new Runnable() {
					public void run() {
						String text = evtTime;
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
						talker.playSilence(2000, TextToSpeech.QUEUE_ADD, null);
						talker.speak(text, TextToSpeech.QUEUE_ADD, null);
					}
				};

				handler.postDelayed(r, eventSoundGap * 1000);
			}
		});

		// player.setLooping(true);

		// talker = new TextToSpeech(context, this);

		// coundPoolthing();

		/*
		 * new CountDownTimer(2000, 1000) { public void onTick(long
		 * millisUntilFinished) { player.start(); } public void onFinish() {
		 * player.stop(); player.release();
		 * 
		 * Handler handler = new Handler(); final Runnable r = new Runnable() {
		 * public void run() { String text = evtTime; //talker.speak(text,
		 * TextToSpeech.QUEUE_FLUSH, null); talker.speak(text,
		 * TextToSpeech.QUEUE_ADD, null); } };
		 * 
		 * handler.postDelayed(r, eventSoundGap * 1000); } }.start();
		 */

	}

	@Override
	public void onInit(int arg0) {
		// TODO Auto-generated method stub

	}

	public void coundPoolthing() {

		sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
		iTmp = sp.load(context, R.raw.alarm, 1);
		iTmp1 = sp.load(context, R.raw.alarm, 2);
		iTmp2 = sp.load(context, R.raw.alarm, 3);

		sp.play(iTmp, 1, 1, 0, 0, 1);

		PlayTextToSpeech pts = new PlayTextToSpeech(context, activity, evtTime);
		pts.playVoice();

		sp.play(iTmp1, 1, 1, 0, 0, 1);

		PlayTextToSpeech pts1 = new PlayTextToSpeech(context, activity, evtTime);
		pts1.playVoice();

		sp.play(iTmp2, 1, 1, 0, 0, 1);

		PlayTextToSpeech pts2 = new PlayTextToSpeech(context, activity, evtTime);
		pts2.playVoice();

		MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.alarm);

		mPlayer.start();

	}

}
